package com.sugar.queryinfo.module.business.controller.admin.userbalancerecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.userbalancerecord.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.userbalancerecord.UserBalanceRecordDO;
import com.sugar.queryinfo.module.business.service.userbalancerecord.UserBalanceRecordService;

@Tag(name = "管理后台 - 余额变动记录")
@RestController
@RequestMapping("/business/user-balance-record")
@Validated
public class UserBalanceRecordController {

    @Resource
    private UserBalanceRecordService userBalanceRecordService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除余额变动记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-balance-record:delete')")
    public CommonResult<Boolean> deleteUserBalanceRecord(@RequestParam("id") Long id) {
        userBalanceRecordService.deleteUserBalanceRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除余额变动记录")
                @PreAuthorize("@ss.hasPermission('business:user-balance-record:delete')")
    public CommonResult<Boolean> deleteUserBalanceRecordList(@RequestParam("ids") List<Long> ids) {
        userBalanceRecordService.deleteUserBalanceRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得余额变动记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-balance-record:query')")
    public CommonResult<UserBalanceRecordRespVO> getUserBalanceRecord(@RequestParam("id") Long id) {
        UserBalanceRecordDO userBalanceRecord = userBalanceRecordService.getUserBalanceRecord(id);
        return success(BeanUtils.toBean(userBalanceRecord, UserBalanceRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得余额变动记录分页")
    @PreAuthorize("@ss.hasPermission('business:user-balance-record:query')")
    public CommonResult<PageResult<UserBalanceRecordRespVO>> getUserBalanceRecordPage(@Valid UserBalanceRecordPageReqVO pageReqVO) {
        return success(userBalanceRecordService.getUserBalanceRecordPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出余额变动记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-balance-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserBalanceRecordExcel(@Valid UserBalanceRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "余额变动记录.xls", "数据", UserBalanceRecordRespVO.class,
                userBalanceRecordService.getUserBalanceRecordPage(pageReqVO).getList());
    }

}